﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Mvc;
using System.Web.UI.WebControls;
using Common;
using Common.Attribute;
using Model;
using Model.DtoModel;
using Model.EasyUiModel;
using Model.ViewModel;
using MvcOa.Helper;
using MvcOa.Logic.Admin.Filter;

namespace MvcOa.Logic.Admin
{
    public class AdminController:Controller
    {
        [HttpGet]
        public ActionResult Login()
        {
            if (!string.IsNullOrEmpty(CookieHelper.GetValue("uid")))
            {
                return RedirectToAction("Index");
            }
            return View(new LoginViewModel(){uLoginName = "admin",uPwd = "123456"});
        }

        [HttpPost]
        public ActionResult Login(LoginViewModel viewModel)
        {
            MessageModel messageModel = new MessageModel();
            if (ModelState.IsValid)
            {
                messageModel=OperationContext.Current.IBllSession.IUserInfoBll.Login(viewModel);
                if (!messageModel.IsSuccess ) return Json(messageModel);
                
                UserInfo userInfo = messageModel.Data as UserInfo;
                if (viewModel.IsChecked&&userInfo!=null)
                {
                    //存入Cookie
                    CookieHelper.SetValue("uid", userInfo.uId);
                }

                //UserInfo存入全局变量
                OperationContext.Current.CurrentUser = userInfo;
                //Permission存入全局变量
                List<Permission> listpPermissions =
                    OperationContext.Current.IBllSession.IUserInfoBll.GetUserPermission(userInfo.uId).ToList().Select(m => m.ToPoco()).ToList();
                OperationContext.Current.ListPermissions = listpPermissions;
            }
            else
            {
                messageModel.IsSuccess = false;
                messageModel.Message = "数据格式有问题";
            }
            return Json(messageModel);
        }

        [HttpGet]
        //暂时注释权限验证
        [AdminAuthorize]
        public ActionResult Index()
        {
            return View();
        }

        public ActionResult NoPermission()
        {
            return View();
        }

        public ActionResult Test()
        {
            return View();
        }
    }
}
